package com.woniuedu.article.controller;

import com.woniuedu.article.dto.AddDto;
import com.woniuedu.article.dto.LikeDto;
import com.woniuedu.article.entity.EduArticle;
import com.woniuedu.article.entity.EduArticleComment;
import com.woniuedu.article.service.ArticleService;
import com.woniuedu.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Why
 * @Date: 2022/4/20 10:06
 * @Description: TODO
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired(required = false)
    ArticleService articleService;

    @GetMapping("/findArticle/{pageNums}/{pageSize}")
    public R findArticle(@PathVariable int pageNums,@PathVariable int pageSize){
        return articleService.findArticle(pageNums, pageSize);
    }


    /**
     * @Author: Why
     * @Date: 2022/4/21 11:20
     * @param addDto 
     * @return com.woniuedu.commonutils.R 
     * @Description: 新增文章
     */
    @PostMapping("/add")
    public R addArticle(@RequestBody AddDto addDto) {
        return  articleService.addArticle(addDto);
    }

    /**
     * @Author: Why
     * @Date: 2022/4/21 11:20
     * @param id 
     * @return com.woniuedu.commonutils.R 
     * @Description: 查找文章通过id
     */
    @GetMapping("/find/{id}")
    public R findArticle(@PathVariable("id") Long id){
//        EduArticle eduArticle = articleService.getById(id);
//        if (eduArticle != null){
//            return R.ok().code(200).message("获取文章成功").data("EduArticle",eduArticle);
//        } else {
//            return R.error().code(500).message("不存在该文章");
//        }
        return articleService.findById(id);
    }

    /**
     * @Author: Why
     * @Date: 2022/4/21 17:13
     * @param
     * @return com.woniuedu.commonutils.R
     * @Description: 获取所有的分类和标签
     */
    @GetMapping("/findAllClassAndLable")
    public R findAllClassAndLable(){
        return articleService.findAllClassAndLable();
    }

    @PostMapping("/like")
    public R like(@RequestBody LikeDto likeDto){
      return articleService.like(likeDto);
    }

    @PostMapping("/unlike")
    public R unLike(@RequestBody LikeDto likeDto){
        return articleService.unlike(likeDto);
    }

    @PostMapping("/addComment")
    public R addComment(@RequestBody EduArticleComment comment){
        return articleService.addComment(comment);
    }

}
